#!/usr/bin/env python
#
# Copyright 2011 Justin Bruce Van Horne <justinvh@gmail.com>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Defines the idea of an entity.

 Entity: The base entity object of any interactable objects in the engine.
"""

import pyglet

from animation import Character
from pyglet.window import key

EAST = 0
WEST = 1
NORTH = 2
SOUTH = 3
NORTH_EAST = 4
NORTH_WEST = 5
SOUTH_EAST = 6
SOUTH_WEST = 7


class Entity(object):
    """A player is one that is connected to the game via a local device or
    over a connection. They are entities that can need a host. Players are
    an abstract class in that they should be implemented as individual
    characters."""
    config = None
    player = False
    keys = None
    anim = None
    batch = None
    x = 0
    y = 0

    def __init__(self):
        """Initialize the player."""
        self.character = Character(animations=self.anim, batch=self.batch)
        pyglet.clock.schedule_interval(self.update, 1 / 18.0)

    def update(self, dt):
        """A think method for the entity."""
        if not self.player or not self.keys:
            return

        keys = self.keys

        if keys[key.W] and keys[key.D]:
            self.move(NORTH_EAST)
        elif keys[key.W] and keys[key.A]:
            self.move(NORTH_WEST)
        elif keys[key.S] and keys[key.D]:
            self.move(SOUTH_EAST)
        elif keys[key.S] and keys[key.A]:
            self.move(SOUTH_WEST)
        elif keys[key.W]:
            self.move(NORTH)
        elif keys[key.S]:
            self.move(SOUTH)
        elif keys[key.D]:
            self.move(EAST)
        elif keys[key.A]:
            self.move(WEST)
        else:
            if self.character._frame_index != 3:
                self.character.frame(3)
                self.character.play()

    def move(self, direction):
        """Defines movement"""
        speed = self.config['walk']
        if direction == NORTH:
            self.y += speed
            self.character.switch_animation("move_north")
        elif direction == EAST:
            self.x += speed
            self.character.switch_animation("move_east")
        elif direction == SOUTH:
            self.y -= speed
            self.character.switch_animation("move_south")
        elif direction == WEST:
            self.x -= speed
            self.character.switch_animation("move_west")
        elif direction == NORTH_EAST:
            self.x += speed
            self.y += speed
            self.character.switch_animation("move_north_east")
        elif direction == NORTH_WEST:
            self.x -= speed
            self.y += speed
            self.character.switch_animation("move_north_west")
        elif direction == SOUTH_EAST:
            self.x += speed
            self.y -= speed
            self.character.switch_animation("move_south_east")
        elif direction == SOUTH_WEST:
            self.x -= speed
            self.y -= speed
            self.character.switch_animation("move_south_west")
        self.character.play()
